package cn.kgc.springcloud.controller;

import cn.kgc.springcloud.client.HystrixClient;
import cn.kgc.springcloud.client.OrderClient;
import cn.kgc.springcloud.entity.Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 课工场
 * @date 2023/10/10
 * @description
 */
@RestController
@RequestMapping("user")
@Slf4j
public class UserController {


    @Autowired
    private OrderClient orderClient;

    @Autowired
    private HystrixClient hystrixClient;

    @RequestMapping("m1")
    public String  method(){

        //向order服务发起http请求   order -> m1   http://192.168.1.5:8507/score/m1
        String result = orderClient.method1();
        return " UserService ok  ==请求==>OrderService  ==结果==> "+result;
    }


    @RequestMapping("m2")
    public String  method2(Integer id){

        log.info("id={}",id);
        //向order服务发起http请求
        String result = orderClient.method2(id);

        return " UserService ok  ==请求==>OrderService  ==结果==> "+result;
    }


    @RequestMapping("m3/{id}")
    public String  method3(@PathVariable("id") Integer id){

        log.info("id={}",id);
        //向order服务发起http请求
        String result = orderClient.method3(id);

        return " UserService ok  ==请求==>OrderService  ==结果==> "+result;
    }


    @RequestMapping("m4")
    public String  method4(Integer id,String name){

        log.info("id={}",id);
        //向order服务发起http请求
        String result = orderClient.method4(id,name);

        return " UserService ok  ==请求==>OrderService  ==结果==> "+result;
    }


    @RequestMapping("m5")
    public String  method5(Integer id,String name){

        Order order = new Order();
        order.setName(name);
        order.setId(id);

        log.info("id={}",id);
        //向order服务发起http请求
        String result = orderClient.method5(order);

        return " UserService ok  ==请求==>OrderService  ==结果==> "+result;
    }

    @RequestMapping("m6")
    public String  method6(Integer[] ids){

        log.info("ids={}",ids);
        //向order服务发起http请求
        String result = orderClient.method6(ids);

        return " UserService ok  ==请求==>OrderService  ==结果==> "+result;
    }

    @RequestMapping("m7")
    public String  method7(Integer id){

        //向order服务发起http请求
        Order result = orderClient.method7(id);
        System.out.println(result);
        return " UserService ok  ==请求==>OrderService  ==结果==> "+result;
    }

    @RequestMapping("m8")
    public String  method8(){

        //向order服务发起http请求
        List<Order> orders = orderClient.method8();
        orders.forEach(System.out::println);
        return " UserService ok  ==请求==>OrderService  ==结果==> "+orders;
    }

    @RequestMapping("m9")
    public String  method9(Integer id){

        //向hystrix服务发起http请求
        String result = hystrixClient.method1(id);
        return " UserService ok  ==请求==>OrderService  ==结果==> "+result;
    }

}
